package com.yaodian100.mall.member.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.huangzhimin.contacts.Contact;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.ec.member.entity.ImageValid;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.member.service.SnsService;
import com.yaodian100.mall.member.util.UserData;

/**
 * 
 * @author afra.chou
 * 
 */
public class SnsAction extends ActionSupport {
	@Resource
	private SnsService snsService;
	@Resource
	private MemberService memberService;

	private final String key = "sNsSerVicE";

	private String tp;
	private List<Contact> friends;
	private String friendStr;
	private String pageType;
	private String pageData;
	private String snsType;
	private String contacts;
	private String userName;
	private String password;
	private String prodId;
	private String chk;
	private String ccode;
	private String ccodeResult;

	/**
	 * @return the tp
	 */
	public String getTp() {
		return tp;
	}

	/**
	 * @param tp the tp to set
	 */
	public void setTp(String tp) {
		this.tp = tp;
	}

	/**
	 * @return the friends
	 */
	public List<Contact> getFriends() {
		return friends;
	}

	/**
	 * @param friends the friends to set
	 */
	public void setFriends(List<Contact> friends) {
		this.friends = friends;
	}

	/**
	 * @return the pageType
	 */
	public String getPageType() {
		return pageType;
	}

	/**
	 * @param pageType the pageType to set
	 */
	public void setPageType(String pageType) {
		this.pageType = pageType;
	}

	/**
	 * @return the pageData
	 */
	public String getPageData() {
		return pageData;
	}

	/**
	 * @param pageData the pageData to set
	 */
	public void setPageData(String pageData) {
		this.pageData = pageData;
	}

	/**
	 * @return the snsType
	 */
	public String getSnsType() {
		return snsType;
	}

	/**
	 * @param snsType the snsType to set
	 */
	public void setSnsType(String snsType) {
		this.snsType = snsType;
	}

	/**
	 * @return the contacts
	 */
	public String getContacts() {
		return contacts;
	}

	/**
	 * @param contacts the contacts to set
	 */
	public void setContacts(String contacts) {
		this.contacts = contacts;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the friendStr
	 */
	public String getFriendStr() {
		return friendStr;
	}

	/**
	 * @param friendStr the friendStr to set
	 */
	public void setFriendStr(String friendStr) {
		this.friendStr = friendStr;
	}

	/**
	 * @return the prodId
	 */
	public String getProdId() {
		return prodId;
	}

	/**
	 * @param prodId the prodId to set
	 */
	public void setProdId(String prodId) {
		this.prodId = prodId;
	}

	/**
	 * @return the chk
	 */
	public String getChk() {
		return chk;
	}

	/**
	 * @param chk the chk to set
	 */
	public void setChk(String chk) {
		this.chk = chk;
	}

	/**
	 * @return the ccode
	 */
	public String getCcode() {
		return ccode;
	}

	/**
	 * @param ccode the ccode to set
	 */
	public void setCcode(String ccode) {
		this.ccode = ccode;
	}

	/**
	 * @return the ccodeResult
	 */
	public String getCcodeResult() {
		return ccodeResult;
	}

	/**
	 * @param ccodeResult the ccodeResult to set
	 */
	public void setCcodeResult(String ccodeResult) {
		this.ccodeResult = ccodeResult;
	}

	// 分享页面
	public String sharing() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String referer = request.getHeader("Referer");

		if (StringUtils.isNotBlank(referer)) {
			if (StringUtils.isNotBlank(prodId)) {
				setPageType("PRD");
				setPageData(prodId);
			} else if (referer.indexOf("productId") > 0) {
				setPageType("PRD");
				setPageData(prodId);
			} else {
				setPageType("MKT");
				setPageData(request.getHeader("Referer"));
			}

			setChk(DigestUtils.md5Hex(pageType + pageData + key));
		}

		return "sharing";
	}

	// 登入页面
	public String loginMail() {
		return "login";
	}

	// 取得联络人资料
	public String getFriendList() {
		friends = snsService.getContacts(userName, password, tp);
		JSON json = JSONSerializer.toJSON(friends);

		friendStr = json.toString();

		return "jsonFriendList";
	}

	// 联络人列表页面
	public String fList() {
		friends = new ArrayList<Contact>();

		JSONArray json = (JSONArray) JSONSerializer.toJSON(friendStr);
		for (Iterator<JSONObject> it = json.iterator(); it.hasNext();) {
			JSONObject contactInfo = it.next();
			Contact contact = new Contact();
			contact.setEmail(contactInfo.getString("email"));
			contact.setUsername(contactInfo.getString("username"));
			friends.add(contact);
		}

		return "friendList";
	}

	public String sendMail() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);

		String referer = request.getHeader("Referer");

		if (StringUtils.isNotBlank(referer)) {

			String chkSign = DigestUtils.md5Hex(pageType + pageData + key);
			if (!chkSign.equalsIgnoreCase(chk)) {
				addActionMessage("分享资料不正确!");
				return sharing();
			} else {

				snsService.sendMail(contacts, pageType, pageData, snsType);
			}
		}
		try {
			response.sendRedirect("sent.do");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "sent";
	}

	public String sent() {
		return "sent";
	}

	public String jsonChkValidateNum() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		// member = memberService.getMemberByLoginId(userData.getLoginId());
		if (!checkCode(userData.getImageValid(), ccode)) {
			ccodeResult = "error";
		} else {
			ccodeResult = "ok";
		}
		return "jsonChkValidateNum";
	}

	private boolean checkCode(String imageId, String inputCode) {
		ImageValid imageValid = new ImageValid();
		imageValid.setId(imageId);
		imageValid.setValidData(inputCode);
		try {
			memberService.isImageValid(imageValid);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}